﻿using DevExpress.Web.ASPxTreeView;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Web.UI.WebControls;
namespace EntryExit
{
    public partial class LeaveCalendar : BasePage
    {
        public Dictionary<string, int> CachedStatusData { get; set; }
        public Employee SelectedEmployee { get; set; }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this.Master is MainMaster)
            {
                ((MainMaster)this.Master).MasterTreeViewNodeSelected += LeaveCalendar_MasterTreeViewNodeSelected;
            }
        }

        void LeaveCalendar_MasterTreeViewNodeSelected(object source, TreeViewNodeEventArgs e)
        {
            SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
            ReInitDataCache();
        }
        private void ReInitDataCache()
        {
            SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
            Session[SessionKeys.CACHED_LEAVE_CALENDAR_DATA] = null;
            Session[SessionKeys.LEAVE_CALENDAR_SHOWING_DATE] = DateTime.MinValue;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                leaveCalendar.VisibleDate = new DateTime(DateTime.Now.Year, 1, 1);
                ReInitDataCache();
            }
        }

        private void CacheLeaveData()
        {
            SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
            leaveViewerPanel.HeaderText = "Leaves : " + SelectedEmployee.EmployeeName;
            Session[SessionKeys.LEAVE_CALENDAR_SHOWING_DATE] = leaveCalendar.VisibleDate;
            Session[SessionKeys.CACHED_LEAVE_CALENDAR_DATA] = EmployeeManagerContext.GetLeavesMarked(leaveCalendar.VisibleDate.AddYears(-1),
                leaveCalendar.VisibleDate.AddYears(1), SelectedEmployee.LPN);
        }

        protected void leaveCalendar_DayRender(object sender, DevExpress.Web.ASPxEditors.DayRenderEventArgs e)
        {
            try
            {
                if ((DateTime)Session[SessionKeys.LEAVE_CALENDAR_SHOWING_DATE] != leaveCalendar.VisibleDate)
                {
                    CacheLeaveData();
                }
                int? statusIdentifier = GetStatusIdForDay(e.Day.DateTime);
                if (statusIdentifier != null)
                {
                    switch (statusIdentifier)
                    {
                        case UIConstants.IDENTIFIER_NO_DATA_BEFORE_DOJ:
                            e.Style.BackColor = Color.Aqua;
                            e.Day.ToolTip = "No data, Reason : Before DOJ";
                            break;
                        case UIConstants.IDENTIFIER_NO_LEAVE_DATA_AVAILABLE:
                            e.Style.BackColor = Color.ForestGreen;
                            e.Day.ToolTip = "Defaulted to Working, Reason : No data";
                            break;
                        case UIConstants.IDENTIFIER_NO_LEAVE_DATA_AVAILABLE_SUN_OR_SAT:
                            e.Style.BackColor = Color.Salmon;
                            e.Day.ToolTip = "Defaulted to Offday, Reason : No data";
                            break;
                        case UIConstants.IDENTIFIER_NO_LEAVE_DATA_AVAILABLE_IN_FUTURE:
                            //Do nothing
                            break;
                        default:
                            LeaveLegend leaveType = (LeaveLegend)statusIdentifier;
                            switch (leaveType)
                            {
                                case LeaveLegend.Working:
                                    e.Style.BackColor = Color.LightGreen;
                                    e.Day.ToolTip = "Working";
                                    break;
                                case LeaveLegend.OffDay:
                                    e.Style.BackColor = Color.LightSalmon;
                                    e.Day.ToolTip = "Off Day";
                                    break;
                                case LeaveLegend.PublicHoliday:
                                    e.Style.BackColor = Color.LightPink;
                                    e.Day.ToolTip = "Public Holiday";
                                    break;
                                case LeaveLegend.CompensatoryOff:
                                    e.Style.BackColor = Color.LightSkyBlue;
                                    e.Day.ToolTip = "Compensatory Off";
                                    break;
                                case LeaveLegend.SickLeave:
                                    e.Style.BackColor = Color.LightGray;
                                    e.Day.ToolTip = "Sick Leave";
                                    break;
                                case LeaveLegend.OptionalHoliday:
                                    e.Style.BackColor = Color.LightCyan;
                                    e.Day.ToolTip = "Optional Holiday";
                                    break;
                                case LeaveLegend.CasualLeave:
                                    e.Style.BackColor = Color.Wheat;
                                    e.Day.ToolTip = "Casual Leave";
                                    break;
                                case LeaveLegend.LossOfPay:
                                    e.Style.BackColor = Color.Yellow;
                                    e.Day.ToolTip = "Loss Of Pay";
                                    break;
                                case LeaveLegend.PaternityLeave:
                                    e.Style.BackColor = Color.LightSteelBlue;
                                    e.Day.ToolTip = "Paternity Leave";
                                    break;
                                case LeaveLegend.MaternityLeave:
                                    e.Style.BackColor = Color.Magenta;
                                    e.Day.ToolTip = "Maternity Leave";
                                    break;
                                case LeaveLegend.StudyLeave:
                                    e.Style.BackColor = Color.Linen;
                                    e.Day.ToolTip = "Study Leave";
                                    break;
                                case LeaveLegend.CompensatoryOffEligible:
                                    e.Style.BackColor = Color.MediumAquamarine;
                                    e.Day.ToolTip = "Compensatory Off Eligible";
                                    break;
                                case LeaveLegend.CasualLeaveHalfDay:
                                    e.Style.BackColor = Color.Maroon;
                                    e.Day.ToolTip = "Casual Leave Half Day";
                                    break;
                                case LeaveLegend.SickLeaveHalfDay:
                                    e.Style.BackColor = Color.MediumPurple;
                                    e.Day.ToolTip = "Sick Leave Half Day";
                                    break;
                                case LeaveLegend.OptionalHolidayLeave:
                                    e.Style.BackColor = Color.MediumTurquoise;
                                    e.Day.ToolTip = "Optional Holiday (Leave)";
                                    break;
                                default:
                                    e.Style.BackColor = Color.Red;
                                    e.Day.ToolTip = "Undefined Leave";
                                    break;
                            }
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private int? GetStatusIdForDay(DateTime statusDate)
        {
            string dayKey = statusDate.ToShortDateString();
            if (Session[SessionKeys.CACHED_LEAVE_CALENDAR_DATA] == null)
            {
                return null;
            }
            else
            {
                if (SelectedEmployee.JoiningDate > statusDate)
                {
                    return UIConstants.IDENTIFIER_NO_DATA_BEFORE_DOJ;
                }
                if (CachedStatusData == null)
                {
                    CachedStatusData = Session[SessionKeys.CACHED_LEAVE_CALENDAR_DATA] as Dictionary<string, int>;
                }
                if (!CachedStatusData.ContainsKey(dayKey))
                {
                    if (statusDate < DateTime.Now)
                    {
                        if (statusDate.DayOfWeek == DayOfWeek.Sunday || statusDate.DayOfWeek == DayOfWeek.Saturday)
                        {
                            return UIConstants.IDENTIFIER_NO_LEAVE_DATA_AVAILABLE_SUN_OR_SAT;
                        }
                        else
                        {
                            return UIConstants.IDENTIFIER_NO_LEAVE_DATA_AVAILABLE;
                        }
                    }
                    else
                    {
                        return UIConstants.IDENTIFIER_NO_LEAVE_DATA_AVAILABLE_IN_FUTURE;
                    }
                }
                return CachedStatusData[statusDate.ToShortDateString()];
            }
        }
    }
} 